// index.js
import { productsRequest,wxauthorRequest } from "./indexApi"

Page({
  data: {
    footList: [
      // {
      //   previewimage: "../../images/test.jpg",
      //   name: "食品1",
      //   tip: "贼好吃啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
      //   price: 10,
      //   sales: 0,
      //   unit: "份"
      // },
      // {
      //   previewimage: "../../images/test.jpg",
      //   name: "食品1",
      //   price: 10,
      //   sales: 0,
      //   unit: "份"
      // },
    ],
  },
  // 获取登录授权
  async login(code) {
    let requestBody = {
      wxcode: code
    }
    let res = await wxauthorRequest(requestBody)
      if (res.status) {
      wx.setStorageSync('token', res.result.token)
    } else {
      console.log("请求失败");
    }
  },
  // 获取列表
  async products() {
    let requestBody = {}
    let res = await productsRequest(requestBody)
    if (res.status) {
      this.setData({
        footList: res.result
      })
    } else {
      console.log("请求失败");

    }
  },

  // 点击跳转详情
  gotoDetail(e) {
    console.log(e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/index/detail/detail?id='+item.id,
    })
  },

  loginXiaoshiji() {
    let that = this
    wx.login({
      success (res) {
        if (res.code) {
          that.login(res.code)
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },


  // 生命周期
  onLoad: function (options) {
    // console.log("-------page-----onLoad------------");
    this.loginXiaoshiji()
    this.products()
  },
  onReady: function () {
    console.log("-------page-----onRe/ady------------");
  },
  onShow: function () {
    // console.log("-------page-----onShow------------");
  },
  onHide: function () {
    // console.log("-------page-----onHide------------");
  },
  onUnload: function () {
    // console.log("-------page-----onUnload------------");
  }
})
